define(function(require,exports,module){
	var name_flag = false;
	var email_flag = false;
	var password_flag = false;
	var repassword_flag = false;
	exports.init = function(){
		//加载普通用户第一页
		ajax_load_user_first();
		//点击换页
		ajax_load_user_aclick();
		//全选
		user_choose_all();
		//single delete
		user_single_delete();
		//mutil  delete
		user_mutil_delete();

		//加载管理员管理
		ajax_load_admin_first();
		//添加管理员
		ajax_load_admin_register();
		//验证admin表单
		check_admin_register_form();

	}	
	exports.operate = function(){

	}
	exports.win_resize = function(){

	}
	function ajax_load_user_first(){
		$.get(backend_var.ajaxLoadUserListUrl,{p:1,flag:'N'},function(e){
			$('.user_replace_div').html(e);
		},'html');
	}
	function ajax_load_user_aclick(){
		$(".user_page_show a").live('click',function(){
			var a_class = $(this).attr('class');
			var page_num;
			if(a_class == 'num'){
				page_num = $(this).text();
			}
			if(a_class == 'next'){
				page_num = Number($(".user_page_show .current").text()) + 1;

			}
			if(a_class == 'prev'){
				page_num = $(".food_page_show .current").text() - 1;
			}
			//这里的'p'参数是与page分页类中的   //分页参数名 一致的
			data = {p:page_num,flag:'N'};
			$.get(backend_var.ajaxLoadUserListUrl,data,function(html){
				$(".user_replace_div").html(html);
				// food_list_img_fancybox();
			},'html');
			return false;
		});
	}
	function user_choose_all(){
		$("input[name='user_all']").live('click',function(){
			var checkbox_status = $(this).attr("checked");
			if(checkbox_status == 'checked'){
				$("input[name='user_id[]']").attr('checked','checked');
			}else{
				$("input[name='user_id[]']").removeAttr('checked');
			}
		});
	}
	function user_single_delete(){
		$(".user_delete").live('click',function(){
			var obj = $(this).parents("tr");
			var data = {
				user_id:$(this).parents("tr").attr("user-id")
			}
			$.post(backend_var.ajaxUserDeleteUrl,data,function(e){
				if(e.status){
					obj.fadeOut(400);
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});
				}
			},'json');
		});
	}
	function user_mutil_delete(){
		$(".user_multi_delete").live('click',function(){
			var data = $("form[name='user_mutil_operate']").serializeArray();
			$.post(backend_var.ajaxUserDeleteUrl,data,function(e){
				if(e.status){
					for (var i = e.data.length - 1; i >= 0; i--) {
					$(".user_list tr[user-id="+e.data[i]+"]").fadeOut(400);
				};
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});
				}
			},'json');
		});
	}

	function ajax_load_admin_first(){
		$(".usermanage .main_card_content_ul li[target-div=2]").live('click',function(){
			$.get(backend_var.ajaxLoadUserListUrl,{p:1,flag:'Y'},function(e){
				$('.admin_replace_div').html(e);
			},'html');
		})
	}
	function ajax_load_admin_register(){
		$(".usermanage .main_card_content_ul li[target-div=3]").live('click',function(){
			$.get(backend_var.ajaxLoadAdminRegisterUrl,{},function(e){
				$('.admin_register_div').html(e);
			},'html');
		})
	}

	function check_admin_register_form(){
		check_name();
		check_email();
		check_password();
		$(".admin_register_form_submit").live('click',function(){
			if(!check_name() && !check_email() && !check_password()){
				$.Zebra_Dialog('请正确填写信息!', {
				    'type':'error',
				    'buttons':  ['确定'],
				    'onClose':  function(caption) {	
				    }
				});
				return;
			}
			var data = $("form[name='admin_register_form']").serializeArray();
			$.post(backend_var.registerAdminUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'infirmation',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});
					$(".usermanage .main_card_content .main_card_content_ul li").removeClass("action-target-li")
					$(".usermanage .main_card_content .main_card_content_ul li[target-div=2]").addClass("action-target-li");
					$(".usermanage .main_card_content .target-div .target-son").removeClass("target-div-active");
					$(".usermanage .main_card_content .target-div .target-div-2").addClass("target-div-active");
					$(".usermanage .main_card_content .main_card_content_ul li[target-div=2]").trigger('click');
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});
				}
				return;
			})
		})
	}
	function check_name(){
		$("input[name='name']").live('focusout',function(){

			var obj = $(this);
			var name_notice_ob  = $(".name_notice");
			var name_value = $(this).val();
			var data = {
				name:name_value
			}
			if(name_value == ''){
				obj.css('border','1px solid #E73209');
				obj.css('color','#E73209');
				name_notice_ob.text("*姓名不能为空");
				name_notice_ob.css("color",'#E73209');
				name_flag = false;		
				return;
			}
			if(name_value.length < 6 || name_value.length > 20){
				obj.css('border','1px solid #E73209');
				obj.css('color','#E73209');
				name_notice_ob.text("*姓名长度必须6~20位");
				name_notice_ob.css("color",'#E73209');
				name_flag = false;
				return;
			}

			$.post(backend_var.ajaxCheckNameUniqueUrl,data,function(e){
				if(e.status){
					obj.css('border','1px solid #1CA009');
					obj.css('color','#1CA009');
					name_notice_ob.text("");
				}else{
					obj.css('border','1px solid #E73209');
					obj.css('color','#E73209');
					name_notice_ob.text("姓名以被注册");
					name_notice_ob.css("color",'#E73209');
				}
			},'json');
			name_flag = true;
		})
	return name_flag;
	}
	function check_email(){
		$("input[name='email']").live('focusout',function(){
			var obj = $(this);
			var email_notice_obj = $(".email_notice");
			var email_value = $(this).val();
			if(email_value == ''){
				obj.css('border','1px solid #E73209');
				obj.css('color','#E73209');
				email_notice_obj.text("*邮箱不能为空");
				email_notice_obj.css("color",'#E73209');
				email_flag = false;				
				return ;
			}
			var email_reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
			if(!email_reg.test(email_value)){
				obj.css('border','1px solid #E73209');
				obj.css('color','#E73209');
				email_notice_obj.text("*邮箱格式不正确");
				email_notice_obj.css("color",'#E73209');
				email_flag = false;
				return ;				
			}
			var data = {
				email:email_value
			}
			$.post(backend_var.ajaxCheckEmailUniqueUrl,data,function(e){
				if(e.status){
					obj.css('border','1px solid #1CA009');
					obj.css('color','#1CA009');
					email_notice_obj.text("");
					email_flag = true;
				}else{
					obj.css('border','1px solid #E73209');
					obj.css('color','#E73209');
					email_notice_obj.text("邮箱已被注册");
					email_notice_obj.css("color",'#E73209');
					email_flag = false;
				}
			},'json');
			
		})
		return email_flag;
	}
	function check_password(){
		$("input[name='password']").live('focusout',function(){
			var password = $("input[name='password']").val();
			if(password == ''){
				$(".password_notice").text('*密码不能为空');
				$("input[name='password']").css("border","1px solid #E73209");
				$(".password_notice").css("color",'#E73209');
				password_flag = false;
				return ;
			}else{
				$(".password_notice").text('');
				$("input[name='password']").css("border","");
				$(".password_notice").css("color",'');	
				password_flag = true;			
			}
		});
		$("input[name='repassword']").live('focusout',function(){
			var password = $("input[name='password']").val();
			var repassword = $("input[name='repassword']").val();

			if(password == ''){
				$(".password_notice").text('*密码不能为空');
				$("input[name='password']").css("border","1px solid #E73209");
				$(".password_notice").css("color",'#E73209');
				repassword_flag = false;
				return ;
			}
			if(repassword == ''){
				$(".repassword_notice").text('*确认密码不能为空');
				$(".repassword_notice").css("color",'#E73209');
				$("input[name='repassword']").css("border","1px solid #E73209");
				repassword_flag = false;
				return ;
			}
			if(password != repassword){
				$(".repassword_notice").text('*两次密码不一致！');
				$(".repassword_notice").css("color",'#E73209');
				$("input[name='password']").css("border","1px solid #E73209");
				$("input[name='repassword']").css("border","1px solid #E73209");
				repassword_flag = false;
			}else{
				$(".repassword_notice").text('*两次密码一致！');
				$(".repassword_notice").css("color",'#1CA009');
				$("input[name='password']").css("border","1px solid #1CA009");
				$("input[name='repassword']").css("border","1px solid #1CA009");	
				repassword_flag = true;			
			}
		})
		return repassword_flag;
	}
});